var pos = [];
var atSameTimeN;
var isFailed;

module.exports = pos;

cc.Class({
    extends: cc.Component,

    properties: {
        GameUI: cc.Node,

        numPrefab:
        {
            default: null,
            type: cc.Prefab
        },
        num2Prefab: cc.Prefab,

        scoreDisplay: {
            default: null,
            type: cc.Label
        },

        welcome: cc.Animation,
        Anime321: cc.Animation,

        timeDisplay: cc.Label,
        timeIcon: cc.Animation,
        AddMinusSeconds: cc.Animation,
        failScoreDisplay: cc.Label,
        failedUI: cc.Animation,


        atSameN: 0,
        initTime: 0,

    },

    // LIFE-CYCLE CALLBACKS:


    onLoad() {
        window.isPause = false;
        atSameTimeN = this.atSameN;
        window.timerValue = this.initTime;
        window.nowNumber = 1;
        pos.splice(0, pos.length);//清空pos数组的值
        this.score = 0;
        this.nowNumber = 1;
        this.timeDisplay.string = window.timerValue;           //  场景文本框显示

        var animGameUI = this.GameUI.getComponent(cc.Animation);
        var animWelcome = this.welcome.getComponent(cc.Animation);
        animGameUI.play('guideUI_load');
        animWelcome.play('welcome1');
    },

    scene2() {
        for (let i = 0; i < 3; i++) {
            this.createNewNumber2();
        }
    },

    scene3() {
        cc.find('Canvas/welcome2').getComponent(cc.Animation).play('welcome2-d');
        // this.scheduleOnce(function () { cc.find('Canvas/welcome2').active = false }, 1)
        cc.find('Canvas/welcome3').active = true;
        cc.find('Canvas/welcome3').getComponent(cc.Animation).play('welcome3');
        this.score = 0;
    },

    scene4() {
        // cc.log("JUMP");
        cc.find('Canvas/welcome').getComponent(cc.Animation).play('welcome1-d');
        atSameTimeN = this.atSameN;
        window.timerValue = 20;
        window.nowNumber = 1;
        // window.now=1;
        pos.splice(0, pos.length);//清空pos数组的值
        isFailed = 0;//重置状态
        this.score = 0;
        this.scoreDisplay.string = 0;
        this.nowNumber = 1;
        this.timeDisplay.string = window.timerValue;           //  场景文本框显示
        var anim321 = this.Anime321.getComponent(cc.Animation);

        var anim321State = anim321.getAnimationState('321');
        this.Anime321.node.active = true;
        anim321.play('321');
        if (anim321State) {
            anim321State.on('stop', (event) => {
                // 处理停止播放时的逻辑
                for (let i = 0; i < atSameTimeN; i++) {
                    this.createNewNumber();
                }
                if (window.timerValue >= 0) {
                    this.schedule(function () {
                        this.jishi();
                    }, 1);
                }
                anim321State.off('stop', (event), this);
            }, this);
        }
    },

    jishi() {                // 倒计时算法
        if (window.timerValue >= 1 && window.isPause == false) {
            window.timerValue = window.timerValue - 1;
            this.timeDisplay.string = window.timerValue;    //场景中文本框显示 
            // cc.log("window.timerValue=" + window.timerValue);
        }
    },

    changetimer(newtime, addorminus) {
        this.timeDisplay.string = newtime;
        var animUpdateTime = this.timeIcon.getComponent(cc.Animation);
        var animAddMinusSeconds = this.AddMinusSeconds.getComponent(cc.Animation);
        if (addorminus == 1) {
            animUpdateTime.play('update_time');
            this.AddMinusSeconds.getComponent(cc.Label).string = ('+' + 8 + 's');
            animAddMinusSeconds.play('+-s');
        }
        else {
            animUpdateTime.play('update_time');
            this.AddMinusSeconds.getComponent(cc.Label).string = ('-' + 5 + 's');
            animAddMinusSeconds.play('+-s');
        }
    },
    update(dt) {
        if (window.timerValue <= 0 && isFailed == 0) {
            isFailed = 1;
            this.failed();
        }
        if (window.nowNumber == 4) {
            // cc.log("should goto Scene3");
            this.scene3();
            window.nowNumber = -1;
        }
    },

    failed() {
        this.failScoreDisplay.string = this.score;
        console.log("Fail");
        var animFailedUI = this.failedUI.getComponent(cc.Animation);
        animFailedUI.play('failedUI_appear');
    },


    createNewNumber: function () {
        var newNum = cc.instantiate(this.num2Prefab);
        newNum.getComponent('Number2').game = this;
        newNum.parent = this.node; // 将生成的敌人加入节点树
        newNum.getComponent("Number2").init();
        var label = newNum.getChildByName('label');
        label.getComponent(cc.Label).string = this.nowNumber;
        this.nowNumber++;
    },

    createNewNumber2: function () {
        var newNum = cc.instantiate(this.num2Prefab);
        newNum.getComponent('Number2').game = this;
        newNum.parent = this.node;
        newNum.getComponent("Number2").init2();
        var label = newNum.getChildByName('label');
        label.getComponent(cc.Label).string = this.nowNumber;
        this.nowNumber++;
    },

    addScore: function () {
        this.score += 1;
        this.scoreDisplay.string = this.score;
    },
});


